
// function xxxxx(参数列表){
//     方法体
// }

function sayHi(name:string,age:number):string{
    return `hello  ${name},几年 ${age}岁`
}

let name:string = '张飞'
let age:number = 18
let wel:string = sayHi(name,age)
console.log(wel);

//定义一个变量 指向的是function
let myfun = sayHi
myfun(name,age)

/**
 * 定义打招呼的方法
 * @param name  姓名
 * @returns  String
 */
function sayHi2(name:string):string{
    return `hello  ${name}`
}

//我希望myfunc2 代表的是 function(name:string):string
//我希望myfunc2 代表的是 function(name:string,age:number):string
let myfunc2:(name:string)=>string  //ts特有的语法
 myfunc2 = sayHi2
let result = myfunc2(name)
console.log(result);

//操作两个数
let operator2Nums:(a:number,b:number)=>number;


function sum(a:number,b:number):number{
    console.log("两个数加法的值 "+(a+b));
    
    return a + b 
} 

function multiplication(a:number,b:number):number{
    console.log("两个数乘积的值 "+(a*b));
    
    return a * b 
} 

//加法运算
operator2Nums = sum
operator2Nums(10,20)


//乘法
operator2Nums = multiplication
operator2Nums(10,20)


//可选参数的function

function buildName(firstName: string="Hello", lastName?: string) {
    if (lastName) {
        return firstName + ' ' + lastName;
    } else {
        return firstName;
    }
}
let tomcat = buildName('Tom', 'Cat');
let tom = buildName(undefined,'Tom');
console.log(tom);


//参数默认值
function axios(url:string, method:string="get"){
    console.log(`发起网络（${method}）请求,地址 ：${url}`);
    
}

axios('http://localhost:8080/server','get')
axios('http://localhost:8080/baidu','post')
axios('http://localhost:8080/server')



//方法的重载
function reverse(x: number): number;
function reverse(x: string): string;
function reverse(x: number | string): number | string | void {
    if (typeof x === 'number') {
        return Number(x.toString().split('').reverse().join(''));
    } else if (typeof x === 'string') {
        return x.split('').reverse().join('');
    }
}


reverse("123456")
reverse(123456)
















